// 声明式
function double(array) {
    const result = [];
    for (let i = 0; i < array.length; i++) {
        result.push(array[i] * 2);
    }
    return result;
}
function addone(array) {
    const result = [];
    for (let i = 0; i < array.length; i++) {
        result.push(array[i] + 2);
    }
    return result;
}
let input = [1, 2, 3];
let double_result = double(input);
let one_result = addone(input);

let double2 = input => input.map(item => item * 2);
let addone2 = input => input.map(item => item + 1);
let double_result2 = double2(input);
let one_result2 = addone2(input);


// 数据不可变
function arrayPush(arr, newValue) {
    arr.push(newValue);
    return arr;
}
function arrayPush2(arr, newValue) {
    return [...arr, newValue];
}
const original = [1, 2, 3];
const result = arrayPush2(original, 4);
const resultDouble = double(result);
const originalDouble = double(original);

// 纯函数 - I/O
export let MessageNormal = function (message) {
    this._value = message;
    this.display = function () {
        document.getElementById('test').textContent += this._value;
    };
};
export let Message: any = function (x) {
    this._value = x;
};
Message.of = x => new Message(x);
Message.prototype.map = function (fn) {
    return Message.of(fn(this._value));
};


import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-func',
    template: `<span id="test"></span><br /><span id="test2"></span>`
})
export class FuncComponent implements OnInit {
    constructor() { }

    ngOnInit(): void {
        console.log('=========1========');
        console.log(double_result);
        console.log(double_result2);
        console.log('=========2========');
        console.log(result);
        console.log(resultDouble);
        console.log(originalDouble);
        console.log('=========3========');
        const normal = new MessageNormal('output by Normal');
        normal.display();
        Message.of('output by Function').map(x => document.getElementById('test2').textContent += x);
    }
}
